﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using Pheonix.Fashionzoomer.DAL;
using Pheonix.Fashionzoomer.Entity.SqlServer2000;
using Pheonix.CodeLibrary;
using Pheonix.CodeLibrary.Data;
using System.Text;
using System.IO;
using System.Drawing.Imaging;

/// <summary>
/// Summary description for CategoryManager
/// </summary>
public class CategoryManager
{
    public CategoryManager()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public static bool DeleteCategoryImages(List<int> idToDelete)
    {
        try
        {
            CustomCollection<Category> categories = CustomCommand.GetCategoriesById(idToDelete);
            if (categories != null)
            {
                for(int i=0;i<categories.Count;i++)
                {
                    string imageUrl = ((Category)categories[i]).CategoryImage;
                    string folderPath = imageUrl.Substring(0, imageUrl.LastIndexOf('/'));
                    
                    if (Directory.Exists(HttpContext.Current.Server.MapPath(folderPath)))
                        FileAccessUtility.DeleteFolder(folderPath);
                }
                return true;
            }
            return false;
        }
        catch
        {
            return false;
        }
    }

    public static string UploadCategoryImage(string temporaryImageLocation, string categoryName)
    {
        try
        {
            string path;
            string extension = Path.GetExtension(temporaryImageLocation);
            string imageBasePath = "~/upload/image/categoryimage/";
            string imagePath = string.Empty;

            StringBuilder imageUrl = new StringBuilder();
            imageUrl = imageUrl.Append(imageBasePath).Append(categoryName).Append("/");
            imageUrl = imageUrl.Append(categoryName).Append("_max").Append(extension);

            imagePath = HttpContext.Current.Server.MapPath(imageUrl.ToString());
            
            string categoryFolderPath = imageBasePath + categoryName;
            DirectoryInfo categoryFolderInfo = new DirectoryInfo(categoryFolderPath);
            if (!categoryFolderInfo.Exists)
                FileAccessUtility.CreateFolder(categoryFolderPath);
            else
            {
                FileAccessUtility.DeleteFolder(categoryFolderPath);
                FileAccessUtility.CreateFolder(categoryFolderPath);
            }

            string categoryThumbImage = imageUrl.ToString().Replace("max", "thumb");

            if (ImageResizer.SaveAs(temporaryImageLocation, imageUrl.ToString(),
                Utility.GetImageFormat(extension, true)) &&
                ImageResizer.CreateThumbnail(imageUrl.ToString(),
                    categoryThumbImage, 80, false, null))
            {
                path = categoryThumbImage;
                FileAccessUtility.DeleteFile(temporaryImageLocation);
            }
            else
                path = null;

            return path;
        }
        catch
        {
            return null;
        }
    }
}
